package com.example.himalayaproject.find.recommend.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.recommend.Recommend_Serviece.RecommendServices;
import com.example.himalayaproject.find.recommend.bean.Entity2;
import com.example.himalayaproject.find.recommend.bean.ListDiscoveryColumns;
import com.example.himalayaproject.find.recommend.bean.ListEditorRecommendAlbums;
import com.example.himalayaproject.find.recommend.bean.ListEntity;
import com.example.himalayaproject.find.recommend.bean.ListFocusImages;
import com.example.himalayaproject.find.recommend.bean.ListGuess;
import com.example.himalayaproject.find.recommend.bean.ListHotRecommends;
import com.example.himalayaproject.find.recommend.bean.ListSpecialColumn;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Timer;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/05.
 */
//空数据 listSpecial
public class RecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Callback<Entity2>, ViewPager.OnPageChangeListener {
    private Context context;
    //头视图总集合
    private List<ImageView> viewList = new ArrayList<>();
    //头视图集合加入第一张   最后一张
    private List<ImageView> viewLists = new ArrayList<>();
    //标题获取
    private String titieEditor; //小编推荐
    private String titleSpecial;//精品听单
    //no.1
    private static int focusImagesID = 0;
    //no.2
    private int discoveryColumnsID = 1;
    //no.3
    private int guessID = 2;
    //no.4
    private int listEditorID = 3;
    //no3
    private List<ListGuess> listGuesses;
    private RecyclerView recyclerView;
    //no.7
    private int HotRecommendsID = 4;
    //4
    private List<ListEditorRecommendAlbums> listEditor;
    //5
    private List<ListSpecialColumn> listSpecial;
    //7
    private List<ListHotRecommends> list;
    private List<ListEntity> listenList = new ArrayList<>();
    private int focuscurposition = 1;

    private List<ListFocusImages> listFocus;
    //2
    private List<ListDiscoveryColumns> listDiscovery;
    private Timer timer;
    private FocusesViewHolder focusesViewHold;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
//                    focuscurposition++;
//                    focusesViewHold.pager.setCurrentItem(focuscurposition, false);
                    focusesViewHold.pager.setCurrentItem(focusesViewHold.pager.getCurrentItem() + 1);
                    handler.sendEmptyMessageDelayed(1, 4000);
            }
        }
    };


    public RecommendAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        listGuesses = new ArrayList<>();
        listEditor = new ArrayList<>();
        listFocus = new ArrayList<>();
        listSpecial = new ArrayList<>();
        listDiscovery = new ArrayList<>();
        RecommendServices services = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RecommendServices.class);
        services.getHotRecommends().enqueue(this);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == focusImagesID) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.item_01, parent, false);
            return new FocusesViewHolder(rootView);
        } else if (viewType == discoveryColumnsID) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.recommend_item2_recycle, parent, false);
            return new DiscoveryViewHolder(rootView);
        } else if (viewType == guessID) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.recommend_item03_recycle, parent, false);
            return new GussViewHolder(rootView);
        } else if (viewType == listEditorID) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.recommend_item04_recycle, parent, false);
            return new EditorViewHolder(rootView);

        } else {
            View rootView = LayoutInflater.from(context).inflate(R.layout.recommend_item05_recycle, parent, false);
            return new HotViewHolder(rootView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) throws IndexOutOfBoundsException {

        if (position == focusImagesID) {
            if (viewLists!=null&&viewLists.size()>0) {

                focusesViewHold = ((FocusesViewHolder) holder);
                Log.d("16200", viewLists.size() + "viewLists");
                WelcomeAdapter adapter = new WelcomeAdapter(viewLists, context);
                focusesViewHold.pager.setAdapter(adapter);
                focusesViewHold.pager.addOnPageChangeListener(this);
                focusesViewHold.pager.setCurrentItem(1);
                adapter.notifyDataSetChanged();
                //focusesViewHold.pager.addOnAdapterChangeListener(this)
                startTimer();
            }
        } else if (position == discoveryColumnsID) {
            bindDiscoveryViewHolder(holder, position);
        } else if (position == guessID) {
            bindGussViewHolder(holder, position);

        } else if (position == listEditorID) {
            binEditorViewHolder(holder, position);
        } else {
            bindHotViewHolder(holder, position);
        }

    }

    //7
    private void bindHotViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.d("1620", "bindHotViewHolde:position" + position);
        HotViewHolder viewHolder = (HotViewHolder) holder;
        Log.d("1620", "bindHotViewHolder执行了2");
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        Log.d("1620", "bindHotViewHolder执行了3");
        viewHolder.recycleview.setLayoutManager(manager);
        if (position < 20 &&list!=null&&list.size()>0) {
            Log.d("16200",list.size()+"list");
            if (list.get(position).getTitle() != null) {
                viewHolder.text.setText(list.get(position).getTitle());
            }
            if (list.get(position).getList() != null) {
                listenList = (list.get(position)).getList();
            }
            HotApapter apapter = new HotApapter(listenList, context);
            viewHolder.recycleview.setAdapter(apapter);
        }


    }

    //4
    private void binEditorViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (listEditor != null) {
            Log.d("1620", "执行了1");
            EditorViewHolder viewHolder = (EditorViewHolder) holder;
            Log.d("1620", "执行了2");
            GridLayoutManager manager = new GridLayoutManager(context, 3);
            Log.d("1620", "执行了3");
            viewHolder.recycleview.setLayoutManager(manager);
            Log.d("1620", "执行了4");
            EditorApapter apapter = new EditorApapter(listEditor, context);
            Log.d("1620", "执行了5");
            viewHolder.recycleview.setAdapter(apapter);
            Log.d("1620", "执行了6");
        }
    }

    //3
    private void bindGussViewHolder(RecyclerView.ViewHolder holder, int position) {
        GussViewHolder viewHolder = (GussViewHolder) holder;
        if (titieEditor != null) {

            viewHolder.text.setText(titieEditor);
        }
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        viewHolder.recycleview.setLayoutManager(manager);
        GussAdapter adapter = new GussAdapter(listGuesses, context);
        viewHolder.recycleview.setAdapter(adapter);

    }

    //第二个viewpager
    private void bindDiscoveryViewHolder(RecyclerView.ViewHolder holder, int position) {
        DiscoveryAdapter adapter = new DiscoveryAdapter(listDiscovery, context);
        DiscoveryViewHolder viewHolder = (DiscoveryViewHolder) holder;
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        viewHolder.recycleview.setLayoutManager(manager);
        viewHolder.recycleview.setAdapter(adapter);
        if (listDiscovery != null) {

            adapter.addAllDiscovery(listDiscovery);
        }


    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return focusImagesID;
        } else if (position == 1) {
            return discoveryColumnsID;
        } else if (position == 2) {
            return guessID;
        } else if (position == 3) {
            return listEditorID;

        } else {
            return HotRecommendsID;
        }


    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public int getItemCount() {

        return listFocus.size() + listGuesses.size() + listEditor.size() + list.size() + listDiscovery.size();

    }

    public void addAll(Collection<? extends ListHotRecommends> collection) {
        int size = listFocus.size();
        list.addAll(collection);

        notifyItemRangeInserted(size, collection.size());
    }

    public void addAlls(Collection<? extends ListGuess> collection) {
        int size = collection.size();
        listGuesses.addAll(collection);

        notifyItemRangeInserted(size, collection.size());
    }

    @Override
    public void onResponse(Call<Entity2> call, Response<Entity2> response) {
        listEditor = response.body().getEditorRecommendAlbums().getList();
        listFocus = response.body().getFocusImages().getList();
        if (response.body().getFocusImages() != null) {
            for (int i = 0; i < response.body().getFocusImages().getList().size(); i++) {
                ImageView imageView = new ImageView(context);
                Picasso.with(context).load(response.body().getFocusImages().getList().get(i).getPic()).into(imageView);
                viewList.add(imageView);
            }

            viewLists.add(viewList.get(viewList.size() - 1));
            viewLists.addAll(viewList);
            viewLists.add(viewList.get(0));
        }

//        Log.d("1620", "viewLists.size()" + viewLists.size());
        for (int i = 0; i < viewLists.size(); i++) {
            Log.d("1620", viewLists.get(i).toString());
        }


//        Log.d("1620", "viewList.size()" + viewList.size());
        listSpecial = response.body().getSpecialColumn().getListSpecialColumns();
        listDiscovery = response.body().getDiscoveryColumns().getList();
        titieEditor = response.body().getEditorRecommendAlbums().getTitle();
        titleSpecial = response.body().getSpecialColumn().getTitle();
    }

    @Override
    public void onFailure(Call<Entity2> call, Throwable t) {
    }


    public void startTimer() {
        /*timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 1;
                handler.sendEmptyMessage(message.what);
            }
        }, 0, 3000);*/
        new Thread(new Runnable() {
            @Override
            public void run() {

                Message mag = Message.obtain();
                mag.what = 1;
                handler.sendEmptyMessage(mag.what);
            }
        }).start();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //记录选择的位置
        focuscurposition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_IDLE:
                Log.d("1620", "idle");
                if (focuscurposition == 0) {
                    focusesViewHold.pager.setCurrentItem(viewLists.size() - 2, false);
                } else if (focuscurposition == viewLists.size() - 1) {
                    focusesViewHold.pager.setCurrentItem(1, false);
                }
                break;
            case ViewPager.SCROLL_STATE_DRAGGING:
                Log.d("1620", "dragging");
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                Log.d("1620", "settling");
                break;
        }

    }


    public static class FocusesViewHolder extends RecyclerView.ViewHolder {
        private ViewPager pager;

        public FocusesViewHolder(View itemView) {
            super(itemView);
            pager = ((ViewPager) itemView.findViewById(R.id.item01_pager));
        }
    }


    public static class DiscoveryViewHolder extends RecyclerView.ViewHolder {


        private RecyclerView recycleview;

        public DiscoveryViewHolder(View itemView) {
            super(itemView);
            recycleview = ((RecyclerView) itemView.findViewById(R.id.item02_recycle));

        }
    }


    public static class GussViewHolder extends RecyclerView.ViewHolder {


        private RecyclerView recycleview;
        private TextView text;

        public GussViewHolder(View itemView) {
            super(itemView);
            recycleview = ((RecyclerView) itemView.findViewById(R.id.recommend_item03_recycle));
            text = ((TextView) itemView.findViewById(R.id.recommend_item03_recycle_tv));

        }
    }


    private static class EditorViewHolder extends RecyclerView.ViewHolder {
        private TextView text;
        private RecyclerView recycleview;

        public EditorViewHolder(View itemView) {
            super(itemView);
            text = ((TextView) itemView.findViewById(R.id.recommend_item04_recycle_tv));
            recycleview = ((RecyclerView) itemView.findViewById(R.id.recommend_item04_recycle));
        }
    }

    public static class HotViewHolder extends RecyclerView.ViewHolder {


        private RecyclerView recycleview;
        private TextView text;

        public HotViewHolder(View itemView) {
            super(itemView);
            recycleview = ((RecyclerView) itemView.findViewById(R.id.recommend_item05_recycle));
            text = ((TextView) itemView.findViewById(R.id.recommend_item05_recycle_tv));

        }
    }
}
